#include "app_task.h"

typedef struct {
    void (*task) (void);
    unsigned int time;
    unsigned int interval;
} app_task_t;

static app_task_t task_list[] = {
	{app_led_task,					0,      50},
	{app_btn_task,					1000,   50},
	//{app_motor_task,				0,      50},
	//{app_release_task,			0,      200},
	{my_app_release_task,			0,      200},
	
	{app_printf_task,				1000,	1000},
	{app_uart_rx_task,				0,		50},
	{app_time_task,					4000,   1000},	
	{app_plan_task,					1000,   1000},
	//{app_dev_task,				3000,   1000},
	{app_dp_notify_task,			1000,   1000},
	//{app_power_task,				1000,   5000},
	{my_app_power_task,				0,   	1000},
	{app_wakeup_alarm_led_task,		1000,   1000},	
	//{app_factory_task,				500,    1000},
};

static const unsigned char task_counts = sizeof(task_list) / sizeof(app_task_t);
static void app_task_ok(void)
{
    app_led_on_all();
    LL_IWDG_ReloadCounter(IWDG);
    LL_mDelay(1000);
    LL_IWDG_ReloadCounter(IWDG);
    app_led_off_all();
}

void app_task_init(void)
{
	app_factory_init();
	app_led_init();
	app_task_ok();
	//app_motor_init();
	app_btn_init();
	app_wifi_init();
	//app_power_init();
  	if (dev_obj->factory.state == FACTORY_NONE) {
		app_plan_init();
		//app_release_init();
	}
	app_dev_init();   
	app_buzzer_init();
	app_time_rec_init();

	my_app_release_init();
	my_app_power_init();
	//app_uart_tx_wifi_distribution_network();
}

unsigned char app_task_enter_sleep(void)
{
    if (app_dev_is_ok_to_sleep()) {
        return 0x01;
    }

    return 0x00;
}

void app_task_run(void)
{
    while (true) {
        for (unsigned char i = 0; i < task_counts; i++) {
            if (time_after(app_sys_ticks, task_list[i].time)) {
                task_list[i].task();
                task_list[i].time = app_sys_ticks + task_list[i].interval;
            }
        }
		LL_IWDG_ReloadCounter(IWDG);
		if (app_dev_is_ok_to_sleep() == 1)
		{
			app_wifi_set_off();
			app_adc_closed();
			app_motor_standby();
			break;
		}
    }
}
